// page_one/jiankongpz/jiankongpz.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({

  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    heights: "",
    yuming,
    title: '设备配置',
    buttonClicked: false,
    sbbh: '',
    sbyz: '',
    azqy: '',
    ppxh: '', //品牌型号
    dibu_img: '', //底部照片
    wudibu_img: '',
    // area: '',
    // areaList: [{
    //     checked: false,
    //     value: '大厅',
    //   },
    //   {
    //     checked: false,
    //     value: '客厅',
    //   },
    // ],
    beizhu: '',
  },
  // 上传
  shangchuan(e) {
    var that = this;
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        that.setData({
          dibu_img: urls,
          wudibu_img: data.imgUrl
        });
      })
    })
  },
  fangda() {
    console.log('点击了放大！')
    wx.previewImage({
      urls: [this.data.dibu_img]
    })
  },
  huan() {
    var that = this
    wx.showModal({
      title: '您想实现哪种操作？',
      cancelText: '放大',
      confirmText: '重新上传',
      success: function (res) {
        if (res.confirm) {
          console.log("重新上传")
          that.shangchuan()
        } else {
          console.log("放大")
          that.fangda()
        }
      }
    })
  },
  pinpai_xinghao(e) {
    this.setData({
      ppxh: e.detail.value
    })
  },
  // 设备编号
  sbbh(e) {
    this.setData({
      sbbh: e.detail.value
    })
  },
  // 设备验证
  sbyz(e) {
    this.setData({
      sbyz: e.detail.value
    })
  },
  //多选
  // userCheck: function (e) {
  //   let index = e.currentTarget.dataset.id; //获取用户当前选中的索引值
  //   let Box = this.data.areaList
  //   if (Box[index].checked) {
  //     this.data.areaList[index].checked = false;
  //   } else {
  //     this.data.areaList[index].checked = true;
  //   }
  //   this.setData({
  //     areaList: this.data.areaList
  //   })
  //   // 返回用户选中的值
  //   let value = Box.filter((item, index) => {
  //     return item.checked == true;
  //   })
  //   let xditem = ''
  //   value.forEach((item, index) => {
  //     xditem += item.value + ','
  //   })
  //   if (xditem.slice(-1) == ',') {
  //     xditem = xditem.slice(0, xditem.length - 1);
  //   }
  //   this.setData({
  //     area: xditem
  //   })
  //   console.log(this.data.areaList)
  // },

  // 安装区域
  azqy(e) {
    this.setData({
      azqy: e.detail.value
    })
  },
  // 备注
  beizhu(e) {
    this.setData({
      beizhu: e.detail.value
    })
  },
  tijiao() {
    if (this.data.sbbh == '') {
      wx.showToast({
        title: '请输入设备编号',
        icon: 'none'
      })
    } else if (this.data.sbyz == '') {
      wx.showToast({
        title: '请输入设备验证',
        icon: 'none'
      })
    } else if (this.data.area == '') {
      wx.showToast({
        title: '请选择安装区域',
        icon: 'none'
      })
    } else if (this.data.beizhu == '') {
      wx.showToast({
        title: '请输入备注',
        icon: 'none'
      })
    } else {
      this.pzsubmit()
    }
  },
  pzsubmit() {
    let that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'Monitor/shop_save_monitor', {
      id: that.data.id,
      monitor_id: that.data.sbbh,
      monitor_code: that.data.sbyz,
      title: that.data.azqy,
      remark: that.data.beizhu
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          })
        }, 1500)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  scan() {
    this.jiankongimage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        this.getjkxx(data.imgUrl)
      })
    })
  },
  getjkxx(imgUrl) {
    this.myRequest(yuming + 'Besall/monitor_zhuan_text', {
      imgUrl
    }, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        this.setData({
          sbbh: res.data.data.SN,
          sbyz: res.data.data.SC
        })
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
})